package com.gdth.bank.activity;

import java.io.IOException;

import com.gdth.core.annotation.view.ViewInject;
import com.gdth.core.widget.BaseActivity;
import com.gdth.utils.ToolUtil;
import com.gdth.bank.R;

import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.VideoView;

public class VideoActivity extends BaseActivity {
	@ViewInject(id=R.id.header_label_title) TextView txt_header_title;
	@ViewInject(id=R.id.header_btn_left, click="finishClick") TextView txt_header_left;
    @ViewInject(id=R.id.header_btn_right) TextView txt_header_right;
    
    @ViewInject(id=R.id.btn_play, click="play") Button btn_play;
    @ViewInject(id=R.id.btn_stop, click="stop") Button btn_stop;
    
    @ViewInject(id=R.id.vidoView) VideoView mVideoView;
    
    MediaPlayer mMediaMusic = null;
    
    Uri mUri;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_video);
		
		initView();
	}

	private void initView() {
		txt_header_title.setText(R.string.title_activity_video);
		txt_header_right.setVisibility(View.GONE);
		
		Intent intent = this.getIntent();
		String name = intent.getStringExtra("path");
		
		String path = ToolUtil.getFilePath(name);

		mMediaMusic = new MediaPlayer();
		try {
			mMediaMusic.setDataSource(path);
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mUri = Uri.parse(Environment.getExternalStorageDirectory() + "/TalkingFriends/TalkingTom.mp4");

//		mVideoView.setMediaController(new MediaController(this, null));

//		mMediaMusic.setLooping(true);
	}
	
	public void play(View arg0){
		
		if (mMediaMusic != null) {
			try {
				mMediaMusic.prepare();
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
			mMediaMusic.start();  

		}
	}
	
	public void stop(View arg0){
		if(mMediaMusic!=null){  
			mMediaMusic.stop(); 
//			mMediaMusic.release();
		}
	}
	
	@Override  
	protected void onDestroy() { 
		if(mMediaMusic!=null){  
			mMediaMusic.stop();
			mMediaMusic.release();
		}  
		super.onDestroy();
	}
}
